package com.java.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.java.mapper.PayMentMapper;
import com.java.mapper.RefundInfoMapper;
import com.java.model.order.PaymentInfo;
import com.java.model.order.RefundInfo;
import com.java.model.refund.RefundStatusEnum;
import com.java.service.PayMentInfoService;
import com.java.service.RefundInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class RefundInfoServiceImpl extends ServiceImpl<RefundInfoMapper, RefundInfo> implements RefundInfoService {


    @Autowired
    private RefundInfoMapper refundInfoMapper;

    //保存取消订单接口
    @Override
    public RefundInfo saveRefundInfo(PaymentInfo paymentInfo) {
        //orderid 和 payment_type 条件 现查询退款记录表中是否有相同的退款记录
        QueryWrapper<RefundInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id",paymentInfo.getOrderId());
        wrapper.eq("payment_type",paymentInfo.getPaymentType());
        RefundInfo refundInfo = refundInfoMapper.selectOne(wrapper);
        if(refundInfo!=null){
            return refundInfo;
        }
        refundInfo = new RefundInfo();
        refundInfo.setCreateTime(new Date());
        refundInfo.setOrderId(paymentInfo.getOrderId());
        refundInfo.setPaymentType(paymentInfo.getPaymentType());
        refundInfo.setOutTradeNo(paymentInfo.getOutTradeNo());
        refundInfo.setRefundStatus(RefundStatusEnum.UNREFUND.getStatus());//退款中
        refundInfo.setSubject(paymentInfo.getSubject());
        refundInfo.setTotalAmount(paymentInfo.getTotalAmount());
        refundInfoMapper.insert(refundInfo);
        return refundInfo;
    }
}
